﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Spring.Context;
using Spring.Context.Support;
using YiLe.BLL.Service;
using YiLe.Model.Domain;
using YiLe.View.Roles;
using YiLe.View.Permissions;
using YiLe.BLL.ServiceImpl;
using YiLe.Model.Util;
using YiLe.View.Table;
using YiLe.View.Users;

namespace YiLe
{
    public partial class MainForm : Form
    {
        static log4net.ILog logger = log4net.LogManager.GetLogger("Logger");

        private IApplicationContext applicationContext;
        public IApplicationContext ApplicationContext
        {
            get { return applicationContext; }
        }
        //菜单列表
        private IDictionary<YiLe.Model.Domain.Menu, IList<YiLe.Model.Domain.Menu>> menus;


        public MainForm()
        {
            InitializeComponent();
            applicationContext = ContextRegistry.GetContext();
        }

        /// <summary>
        /// 初始化Form
        /// </summary>
        /// <param name="ctx"></param>
        public MainForm(IApplicationContext ctx, IDictionary<YiLe.Model.Domain.Menu, IList<YiLe.Model.Domain.Menu>> menus)
        {
            InitializeComponent();
            //加入form的自适应事件，展示panel中的桌台
            //this.Resize += new EventHandler(this.Form_Resize);
            this.applicationContext = ctx;
            this.menus = menus;
            if (menus.Count > 0)
            {
                //生成一级菜单
                foreach (KeyValuePair<YiLe.Model.Domain.Menu, IList<YiLe.Model.Domain.Menu>> kvp in menus)
                {
                    var firstLevelMenu = new ToolStripMenuItem() { Text = kvp.Key.MenuName, Name = kvp.Key.MenuID.ToString() };//需要增加
                    SetParentMenu(firstLevelMenu);
                    var secondLevelMenu = kvp.Value;//对应的二级菜单

                    //遍历二级菜单
                    foreach (var menu in secondLevelMenu)
                    {
                        var secondMenu = new ToolStripMenuItem() { Text = menu.MenuName, Name = menu.MenuID.ToString() };//需要增加
                        setChildMenu(secondMenu);
                        firstLevelMenu.DropDownItems.Add(secondMenu);
                    }

                    this.menuMain.Items.Add(firstLevelMenu);
                }
            }
        }

        /// <summary>
        /// 设置一级菜单处理事件
        /// </summary>
        /// <param name="firstLevelMenu"></param>
        private void SetParentMenu(ToolStripMenuItem firstLevelMenu)
        {
            //暂无
        }

        /// <summary>
        /// 设置二级菜单处理事件
        /// </summary>
        /// <param name="secondMenu"></param>
        private void setChildMenu(ToolStripMenuItem secondMenu)
        {
            secondMenu.Click += new EventHandler(View_SubMenuClick);
        }

        /// <summary>
        /// 点击二级菜单事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void View_SubMenuClick(object sender, EventArgs e)
        {
            switch ((sender as ToolStripMenuItem).Text)
            {
                case "商品基本信息管理":
                    MessageBox.Show("商品基本信息管理弹出框");
                    break;
                case "商品入库管理":
                    MessageBox.Show("商品入库管理弹出框");
                    break;
                case "商品出库管理":
                    MessageBox.Show("商品出库管理弹出框");
                    break;
                case "商品盘库管理":
                    MessageBox.Show("商品盘库管理弹出框");
                    break;
                case "交班管理":
                    MessageBox.Show("交班管理弹出框");
                    break;
                case "库存统计":
                    MessageBox.Show("库存统计弹出框");
                    break;
                case "交班统计":
                    MessageBox.Show("交班统计弹出框");
                    break;

                case "用户管理":
                    UserListForm ulForm = new UserListForm(applicationContext);
                    ListFormShow(ulForm);
                    ulForm.Show();
                    break;
                case "角色管理":
                    RoleListForm rlForm = new RoleListForm(applicationContext);
                    ListFormShow(rlForm);
                    rlForm.Show();
                    break;
                case "权限管理":
                    PermissionListForm plForm = new PermissionListForm(applicationContext);
                    ListFormShow(plForm);
                    plForm.Show();
                    break;
                case "登录登出管理":
                    MessageBox.Show("登录登出管理弹出框");
                    break;
                case "帮助文档":
                    MessageBox.Show("帮助文档弹出框");
                    break;
                case "关于":
                    MessageBox.Show("关于弹出框");
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 配置ListForm的展示属性，初始化窗口的width和height
        /// </summary>
        /// <param name="form"></param>
        private void ListFormShow(Form form)
        {
            form.Width = Const.LIST_FORM_WIDTH;
            form.Height = Const.LIST_FORM_HEIGHT;
            form.MdiParent = this;
            form.WindowState = FormWindowState.Maximized;
            form.StartPosition = FormStartPosition.CenterScreen;
            //form.FormBorderStyle = FormBorderStyle.SizableToolWindow;
            //form.MaximizeBox = false;
            //form.MinimizeBox = false;
            //form.ControlBox = true;
        }

        /// <summary>
        /// 关闭主程序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }
    }
}
